import { getSysDictDataPage, getSysDictTypeTree } from "@/api/sys/sysDict";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { RootState } from "..";
import { global_const } from "@/utils/constent";

const initialState = {
  dictTypeTree: [] as SysDictTypeEntityType[],
  active_dict_type_id: "",
  isShowDictTypeModal: {
    show: false,
    // 1 新增 2 修改
    mode: 1,
    title: "新增",
    data: {} as Partial<SysDictDataEntityType>,
  },
  dictDataPageLoading: false,
  dictDataList: {
    current: global_const.default_page_current,
    pages: 1,
    records: [],
    total: global_const.default_page_total,
    size: global_const.default_page_size,
  } as PageType<SysDictDataEntityType>,
  dictDataPageParams: {
    dictTypeId: "",
    current: global_const.default_page_current,
    size: global_const.default_page_size,
  },
  isShowDictDataModal: {
    show: false,
    // 1 新增 2 修改
    mode: 1,
    title: "新增",
    data: {} as Partial<SysDictDataEntityType>,
  },
};

export const get_sys_dict_type_tree = createAsyncThunk(
  "get/get_sys_dict_type_tree",
  async () => {
    return await getSysDictTypeTree();
  }
);

export const get_sys_dict_data_page = createAsyncThunk<
  PageType<SysDictDataEntityType>,
  SysDictDataPageParams
>("get/get_sys_dict_data_page", async (params: SysDictDataPageParams) => {
  return await getSysDictDataPage(params);
});

const sysDictSlice = createSlice({
  name: "sysDict",
  initialState,
  reducers: {
    set_is_show_dict_type_modal(state, action) {
      state.isShowDictTypeModal = action.payload;
    },
    set_active_dict_type_id(state, action) {
      state.active_dict_type_id = action.payload;
    },
    set_dict_data_page_params(state, action) {
      state.dictDataPageParams = action.payload;
    },
    set_is_show_dict_data_modal(state, action) {
      state.isShowDictDataModal = action.payload;
    },
  },
  extraReducers(builder) {
    builder
      .addCase(get_sys_dict_type_tree.fulfilled, (state, action) => {
        state.dictTypeTree = action.payload;
      })
      .addCase(get_sys_dict_data_page.pending, (state, action) => {
        state.dictDataPageLoading = true;
      })
      .addCase(get_sys_dict_data_page.fulfilled, (state, action) => {
        state.dictDataPageLoading = false;
        state.dictDataList = action.payload;
      });
  },
});

export const select_dict_type_list = (state: RootState) =>
  state.sysDict.dictTypeTree.map((item: SysDictDataEntityType) => {
    return {
      label: item.dictName,
      value: item.dictCode,
      other: item,
    };
  });

export const select_dict_type_tree = (state: RootState) =>
  state.sysDict.dictTypeTree.map((item: SysDictDataEntityType) => {
    return {
      title: item.dictName,
      key: item.id,
      other: item,
    };
  });

export const select_is_show_dict_type_modal = (state: RootState) =>
  state.sysDict.isShowDictTypeModal;

export const select_active_dict_type_id = (state: RootState) =>
  state.sysDict.active_dict_type_id;

export const select_dict_data_page_params = (state: RootState) =>
  state.sysDict.dictDataPageParams;

export const select_dict_data_page_loading = (state: RootState) =>
  state.sysDict.dictDataPageLoading;

export const select_dict_data_list = (state: RootState) =>
  state.sysDict.dictDataList;

export const select_is_show_dict_data_modal = (state: RootState) =>
  state.sysDict.isShowDictDataModal;

export const {
  set_is_show_dict_type_modal,
  set_active_dict_type_id,
  set_dict_data_page_params,
  set_is_show_dict_data_modal,
} = sysDictSlice.actions;

export default sysDictSlice.reducer;
